<template>
	<!-- 忘记密码 -->
	<view :class="model+'-wrap'">
		<view :class="'p-sticky-'+model">
			<view class="status_bar">

			</view>
			<view class="nav_bar flex align-center justify-between">
				<view @click="$tool.goBack()" class="f0 p-3">
					<image class="fanhui_icon" :src="'/static/images/fanhui_'+model+'.png'" mode=""></image>
				</view>
				<view class="A-Medium" :class="'navTitle_'+model">
					{{i18n.l027}}
				</view>
				<view class="f0 p-3 opacity0">
					<image class="fanhui_icon" :src="'/static/images/fanhui_'+model+'.png'" mode=""></image>
				</view>
			</view>
		</view>
		<view class="h3">

		</view>
		<view class="formWrap px-5 A-Regular">
			<!-- 账号 -->
			<view class="formTitle">
				{{i18n.l028}}
			</view>
			<view class="inputWrap px-3 flex align-center" :class="model=='light'?'inputWrapLight':''">
				<input type="text" v-model="account" :placeholder="i18n.l012 + '/' + i18n.l009" class="flex-1 inputText A-Medium"
					:class="model=='light'?'inputTextLight':''"
					:placeholder-class="'placeholderClass A-Medium',model=='light'?'placeholderClassLight':''">
			</view>
			<!-- 验证码 -->
			<view class="formTitle">
				{{i18n.l013}}
			</view>
			<view class="inputWrap px-3 flex align-center" :class="model=='light'?'inputWrapLight':''">
				<input type="text" v-model="code" :placeholder="i18n.l014" class="flex-1 inputText A-Medium"
					:class="model=='light'?'inputTextLight':''"
					:placeholder-class="'placeholderClass A-Medium',model=='light'?'placeholderClassLight':''">
				<view v-if="codeText == 60" @click="sendCode" class="codeText A-Medium pl-3">
					{{i18n.l015}}
				</view>
				<view v-else class="codeText A-Medium pl-3">{{codeText + 's'}}</view>
			</view>
			<!-- 新密码 -->
			<view class="formTitle">
				{{i18n.l030}}
			</view>
			<view class="inputWrap px-3 flex align-center" :class="model=='light'?'inputWrapLight':''">
				<input :type="showPass?'text':'password'" v-model="password" :placeholder="i18n.l031"
					class="flex-1 inputText A-Medium" :class="model=='light'?'inputTextLight':''"
					:placeholder-class="'placeholderClass A-Medium',model=='light'?'placeholderClassLight':''">
				<view @click="showPass=!showPass" class="f0 pl-3">
					<image v-if="showPass" class="img2" :src="'/static/images/hiddenPass_'+model+'.png'" mode="">
					</image>
					<image v-else class="img2" :src="'/static/images/showPass_'+model+'.png'" mode=""></image>
				</view>
			</view>
			<!-- 确认新密码 -->
			<view class="formTitle">
				{{i18n.l032}}
			</view>
			<view class="inputWrap px-3 flex align-center" :class="model=='light'?'inputWrapLight':''">
				<input :type="showPass2?'text':'password'" v-model="password2" :placeholder="i18n.l033"
					class="flex-1 inputText A-Medium" :class="model=='light'?'inputTextLight':''"
					:placeholder-class="'placeholderClass A-Medium',model=='light'?'placeholderClassLight':''">
				<view @click="showPass2=!showPass2" class="f0 pl-3">
					<image v-if="showPass2" class="img2" :src="'/static/images/hiddenPass_'+model+'.png'" mode="">
					</image>
					<image v-else class="img2" :src="'/static/images/showPass_'+model+'.png'" mode=""></image>
				</view>
			</view>
			<view class="passDesc">
				<image :src="'/static/images/desc_'+model+'.png'" mode=""></image>
				{{i18n.l023}}
			</view>
		</view>
		<!-- 提交按钮 -->
		<view @click="submit" class="btn mx-auto A-Medium mt-8 clickCss">
			{{i18n.l035}}
		</view>
		<view class="codeDesc text-alignC A-Medium mx-6">
			{{i18n10.n159}}
		</view>
	</view>
</template>

<script>
	import {
		getMobileVerifyCode,
		getEmailVerifyCode,
		resetPassword
	} from "@/core/api/user.js";
	export default {
		data() {
			return {
				showPass: false, //是否显示密码
				showPass2: false, //是否显示密码
				account: '', //账号
				password: '', //密码
				password2: '', //密码
				code: '', //验证码
				codeText: 60, //
				codeTimer: null,
				sendFlag: false
			}
		},
		onLoad() {

		},
		onReady() {
			this.$tool.setStatusBarStyle(this.model);
		},
		onShow() {
			this.$tool.setStatusBarStyle(this.model);
			const pages = getCurrentPages();
			const page = pages[pages.length - 1];
			const currentWebview = page.$getAppWebview();
			if(this.model == "dark"){
				currentWebview.setStyle({
					background: "#0E0F11"
				});
			}else{
				currentWebview.setStyle({
					background: "#ffffff"
				});
			}
		},
		computed: {
			i18n() {
				return this.$t('login');
			},
			i18n2() {
				return this.$t('showToast');
			},
			i18n10() {
				return this.$t('newObj');
			},
			model() {
				return this.$store.state.config.model;
			}
		},
		methods: {
			// 发送验证码
			async sendCode() {
				if(this.sendFlag){
					return;
				}
				if (this.account === '') {
					this.$tool.showToast(this.i18n2.st002);
					return;
				}
				this.$tool.showLoading();
				this.sendFlag = true;
				this.sendCodeAfter();
				if (this.account.indexOf('@') == -1) {
					await getMobileVerifyCode({
						phoneNumber: this.account,
					}).then(res => {
						
					})
				} else {
					await getEmailVerifyCode({
						email: this.account,
					}).then(res => {
						
					})
				}
			},
			// 发送验证吗数据处理
			sendCodeAfter() {
				if(!this.codeTimer){
					this.codeTimer = setInterval(e => {
						if (this.codeText == 0) {
							this.codeText = 60;
							clearInterval(this.codeTimer);
							this.codeTimer = null;
							this.sendFlag = false;
							return;
						}
						this.codeText--;
						uni.hideLoading();
					}, 1000)
				}
			},
			submit() {
				if (this.account === '') {
					this.$tool.showToast(this.i18n2.st002);
					return;
				}
				if (this.code === '') {
					this.$tool.showToast(this.i18n2.st003);
					return;
				}
				if (this.password === '') {
					this.$tool.showToast(this.i18n2.st004);
					return;
				}
				if (this.password2 === '') {
					this.$tool.showToast(this.i18n2.st011);
					return;
				}
				if (this.password !== this.password2) {
					this.$tool.showToast(this.i18n2.st012);
					return;
				}
				let data = {
					email: '',
					mobileNumber: '',
					password: this.password,
					verifyCode: this.code,
				}
				if (this.account.indexOf('@') != -1) {
					data.email = this.account;
				} else {
					data.mobileNumber = this.account;
				}
				this.$tool.showLoading();
				resetPassword(data).then(res => {
					uni.hideLoading();
					this.$tool.showToast(this.i18n2.st013);
					setTimeout(res=>{
						this.$tool.goBack();
					},800)
				}).catch(err => {
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.h3 {
		height: 30rpx;
	}

	.formWrap {
		.formTitle {
			font-size: 28rpx;
			color: #7F838D;
			margin-bottom: 22rpx;
		}

		.inputWrap {
			width: 650rpx;
			height: 88rpx;
			background: #16181C;
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			margin-bottom: 35rpx;

			.area {
				font-size: 28rpx;
				color: #AAACB8;
			}

			.areaLight {
				color: #7F838D;
			}

			.inputText {
				font-size: 28rpx;
				color: #F7F7F7;
				// text-align: right;
			}

			.inputTextLight {
				color: #0E0F11;
			}

			.placeholderClass {
				font-size: 28rpx;
				color: #7F838D;
			}

			.placeholderClassLight {
				color: #B5B7BF;
			}

			.img1 {
				width: 15rpx;
				height: 11rpx;
				margin-left: 16rpx;
				margin-right: 42rpx;
			}

			.img2 {
				width: 40rpx;
				height: 40rpx;
			}

			.codeText {
				font-size: 28rpx;
				color: #5951BA;
			}
		}

		.inputWrapLight {
			background: #FFFFFF;
		}

		.passDesc {
			image {
				width: 28rpx;
				height: 28rpx;
				vertical-align: bottom;
				margin-right: 7rpx;
			}

			line-height: 28rpx;
			font-size: 24rpx;
			color: #5951BA;
		}
	}

	.btn {
		width: 650rpx;
		height: 80rpx;
		background: #5951BA;
		border-radius: 74rpx;
		text-align: center;
		line-height: 80rpx;
		font-size: 28rpx;
		color: #F7F7F7;
	}

	.codeDesc {
		margin-top: 40rpx;
		font-size: 28rpx;
		color: #7F838D;
	}
</style>